Skip to content

111. Minimum Depth of Binary Tree#21

Open
n6o wants to merge 1 commit intomainfrom
minimum-depth-of-binary-tree
Open

111. Minimum Depth of Binary Tree#21
n6o wants to merge 1 commit intomainfrom
minimum-depth-of-binary-tree

Conversation

@n6o
Copy link
Copy Markdown
Owner

@n6o n6o commented Feb 22, 2026

今回の問題

Minimum Depth of Binary Tree - LeetCode

使用言語

Go

次に解く問題

Merge Two Binary Trees - LeetCode

depth int
}

nodes := []nodeWithDepth{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodesという名前だと、nodeの配列に感じて、nodeWithDepthの配列だというのは誤解を生むように思えました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。

読む時に違和感を感じないよう nodesWithDepth などのほうが適切そうですね。

@PafsCocotte
Copy link
Copy Markdown

全体的に読みやすかったです。


for len(nodes) > 0 {
current := nodes[0]
// GC対象にするため参照を外す
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このテクニック知らなかったので勉強になりました!メモリリークが起きないようにしているんですね

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これよく知らなかったのですが、ここの SliceTricks に
https://go.dev/wiki/SliceTricks

NOTE If the type of the element is a pointer or a struct with pointer fields, which need to be garbage collected, the above implementations of Cut and Delete have a potential memory leak problem: some elements with values are still referenced by slice a’s underlying array, just not “visible” in the slice. Because the “deleted” value is referenced in the underlying array, the deleted value is still “reachable” during GC, even though the value cannot be referenced by your code. If the underlying array is long-lived, this represents a leak. The following code can fix this problem:

とありました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、Go のスライスは underlying array を持つ構造になっているのでこういうテクニックが必要になるのでしょうね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

みなさまコメントありがとうございます。

おそらく私が最初にこのやり方を知ったのは優先度付きキューの実装例に同様の記載があったことだったかと思います。
記載いただいた通りスライスの構造に由来する処理と理解しています。

https://pkg.go.dev/container/heap

// don't stop the GC from reclaiming the item eventually

Comment on lines +191 to +192
// invalid
return -1
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分ならpanicしちゃうかなあと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。

一般的には error を返すインターフェースにすると思うので、ちょっと書きづらいところではあります。
この練習会では到達不能なコードには panic を使うのが assert 的な意図も伝えられレビュワーにとっては違和感なさそうなので、次回からはその方針にしようと思いました。
(実務では panic はほぼ使わないので、書くのに躊躇する感じでした)

@mamo3gr
Copy link
Copy Markdown

mamo3gr commented Feb 27, 2026

特に違和感ありませんでした 👍 読みやすかったです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants